package com.winning.vsts.merge;

import com.winning.vsts.merge.service.configuration.VstsContext;
import com.winning.vsts.merge.service.domain.VstsProject;
import com.winning.vsts.merge.service.service.VstsClient;
import com.winning.vsts.merge.service.service.VstsProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 自动删除分支信息
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/19
 */
@EnableJpaRepositories(basePackages = "com.winning.vsts.merge.service.jpa")
@SpringBootApplication(scanBasePackages = "com.winning.vsts.merge.service")
public class AutoDeleteBranchApplication implements ApplicationRunner {

    private final static Logger log = LoggerFactory.getLogger(AutoDeleteBranchApplication.class);

    @Autowired
    private VstsProjectService vstsProjectService;

    @Autowired
    private VstsClient vstsClient;

    @Autowired
    private VstsContext vstsContext;


    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .main(AutoDeleteBranchApplication.class)
            .sources(AutoDeleteBranchApplication.class)
            .web(WebApplicationType.NONE)
            .build().run(args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        String deleteBranch = "RC211119";
        log.info("====================开始删除分支{}===================", deleteBranch);
        List<VstsProject> vstsProjects = vstsProjectService.queryVstsProject(null, null);
        for (VstsProject vstsProject : vstsProjects) {
            RestTemplate restTemplate = vstsContext.restTemplate(vstsProject);
            vstsClient.deleteBranch(restTemplate, vstsProject, deleteBranch);
        }
        log.info("====================删除分支{}完成===================", deleteBranch);

    }
}
